package com.cybertron.ironhide.spider.plugin.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import com.cybertron.ironhide.spider.task.Task;
import com.cybertron.ironhide.spider.xml.Target;

public class Util {

	public static Target matchTarget(Task task) throws Exception {
		if (task.isSource) {
			return null;
		}
		Target target = task.site.getTargets().getTarget();
		task.target = target;
		return target;
	}

	public static Collection<String> findAllLinkHref(String html, String hostUrl)
			throws Exception {
		Collection<String> urls = new ArrayList<String>();

		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode node = cleaner.clean(html);
		Object[] ns = node.evaluateXPath("//a[@href]");
		for (Object object : ns) {
			TagNode node2 = (TagNode) object;
			String href = node2.getAttributeByName("href");
			if (href == null || href.trim().length() == 0)
				continue;

			if (!href.startsWith("https://") && !href.startsWith("http://")) {
				StringBuilder sb = new StringBuilder("http://").append(new URL(
						hostUrl).getHost());
				if (!href.startsWith("/"))
					sb.append("/");
				href = sb.append(href).toString();
			}
			// href = URLCanonicalizer.getCanonicalURL(href);
			if (href == null)
				continue;
			if (href.startsWith("mailto:"))
				continue;

			urls.add(href);
		}

		return urls;
	}

	public static String getHtml(String urlString) {
		try {
			StringBuffer html = new StringBuffer();
			URL url = new URL(urlString);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			InputStreamReader isr = new InputStreamReader(conn.getInputStream());
			BufferedReader br = new BufferedReader(isr);
			String temp;
			while ((temp = br.readLine()) != null) {
				html.append(temp).append("\n");
			}
			br.close();
			isr.close();
			return html.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
